<?php
namespace app\yangxt_forum_admin\controller;
use think\Controller;
class Deal extends Controller
{
    public function mesDeal($sid=0)
    {   
        $re=db('section')
        ->select();

        if ($sid==0) {
            $sousuo=input('check');
                $mes =db('mes')
                ->where('mtitle','like','%'.$sousuo.'%')
                               
                ->paginate(6);
                $page1 = $mes ->render();
                $sename='结果';
        }
        else{
        foreach ($re as $key => $value) {
            if ($value['sid']==$sid) {

                $sename=$value['sname'];
                break;
            }
        };
         $mes =db('mes')
                ->view("mes","mtitle,mid,mcontent,unick,mcreateat,sid")
                ->where('sid',$sid)
              
                ->paginate(6);
                $page1 = $mes ->render();
        }
        

        return view("",["re"=>$re,'sename'=>$sename,'mes'=>$mes,'page'=>$page1]);
    }
    public function mesDel()
    {   
        $delmid = input('mid');
        $del =db('mes')
        ->where('mid',$delmid)
        ->delete();
        $userdel =db('res')
        ->where('mid',$delmid)
        ->delete();
        if ($del) {
            $this ->success('删除成功',url("deal/mesdeal",['sid'=>input('sid')]));
        }
        else{
            $this ->error('删除失败',url("deal/mesdeal",['sid'=>input('sid')]));
        };
    }
    public function aDetail()
    {   
        $messel=db('mes')
        ->where('mid',input('mid'))
        ->find();

        $ressel=db('res')
        ->where('mid',input('mid'))
        
        ->select();

        return view("",["mes"=>$messel,'res'=>$ressel]);
    
    }
    public function resDel()
    {   
        $resdel= db('res')
        ->where('rid',input('rid'))
        ->delete();
        if ($resdel) {
            $this ->success('删除回复成功',url("deal/mesdeal",['mid'=>input('mid')]));
        }
        else{
            $this ->error('删除回复失败',url("deal/mesdeal",['mid'=>input('mid')]));
        };
    
    }
    public function userDeal()
    {   
        $user=db('user')
        ->select();
        foreach ($user as $key => &$value) {
            if ($value['ulimit']==0) {
                $value['ulimit']='正常登陆，正常发言';
                $value['color']='';

            }
            elseif($value['ulimit']==1){
                $value['ulimit']='正常登陆，禁止发言';
                $value['color']='color:green';

            }
            elseif($value['ulimit']==2){
                $value['ulimit']='禁止登陆，禁止发言';
                $value['color']='color:red';
            }
        };
        return view('',['user'=>$user]);
    }
    public function userPower()
    {  
        $username=input('unick');
        return view('',['unick'=>$username]);
    }
    public function userPowerModi()
    {  
        $num = (int)input('num');
        $re = db('user')
            ->where('unick',input('unick'))
            ->setField(['ulimit'=>$num]);
            if ($re) {
                $this ->success('修改权限成功',url("deal/userdeal"));
            }
            else{
                $this ->error('修改权限失败',url("deal/userdeal"));
            }

    }
    public function secDeal($find=0)
    {  
        if ($find==0) {
        $sel=db('section')
       
        ->paginate(8);
        $page = $sel ->render();
        }
        else{

        $sel=db('section')
   
        ->where('sname','like','%'.input('check').'%')
        ->paginate(8);
        $page = $sel ->render();
        }
        

        

        return view('',['sel'=>$sel,'page'=>$page]);

    }


       public function secInfo()
    {  
        $sel=db('section')
        ->where('sid',input('sid'))
        ->select();
        return view('',['sel'=>$sel]);

    }
    public function secInfoModi()
    {  
        $oldname=input('sid');
        $nname=input('newname');
        $ntext=input('ntext');

        $re = db('section')
            ->where('sid',$oldname)
            ->setField(['sname'=>$nname,'sremark'=>$ntext]);

            if ($re) {
                $this ->success('修改板块成功',url("deal/secdeal"));
            }
            else{
                $this ->error('修改板块失败',url("deal/secdeal"));
            }

    }

    
    
    public function secDel()
    {  
        $secdel=db('section')
        ->where('sid',input('sid'))
        ->delete();
        $mesre=db('mes')
        ->where('sid',input('sid'))
        ->select();

        foreach ($mesre as $key => $value) {
            $mesid=$value['mid'];
            break;
        };       
        $mesdel=db('mes')
        ->where('sid',input('sid'))
        ->delete(); 
                
         if ($secdel) {
                $this ->success('删除板块成功',url("deal/secdeal"));
            }
            else{
                $this ->error('删除板块失败',url("deal/secdeal"));
            }

    }
    public function secAdd()
    {  
       return view();
    }

    public function secDoAdd()
    {  
       $sec= [
        'sname'=>input('newname'),
        'sremark'=>input('ntext'),
        
       ];
       $secexam=db('section')
       ->where('sname',input('newname'))
       ->select();
       if ($secexam) {
           $this ->error(input('newname').'已存在,添加板块失败',url("deal/secdeal"));
       }
       else{
        $res=db("section")
            ->insert($sec);
            if ($res) {
                $this ->success(input('newname').'添加板块成功',url("deal/secdeal"));
            }
       }
       
    }
    
    public function advDeal()
    {  
        $adv=db('adv')
        ->select();
        foreach ($adv as $key => &$value) {
            if ($value['acheck']==0) {
                $value['acheck']='未审核(点击通过)';
                $value['checkcolor']='red';
                if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
            elseif($value['acheck']==1){
                $value['acheck']='通过审核(点击取消)';
                $value['checkcolor']='red';
                 if ($value['astop']==0) {
                    $value['stopcolor']='';
                }
                else{
                     $value['stopcolor']='red';
                     $value['checkcolor']='';
                }

            }
        };
       return view('',['adv'=>$adv]);
    }

    public function advAdd()
    { 
       return view();
    }

    public function advDoAdd()
    {  
       $file = request()->file("aimg");
       // 存储到指定路径
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
             $adv= [
            'atitle'=>$advtitle,
            'acontent'=>$advcon,
            'aimg'=>$filename,
            'apath'=>$apath,
            'atime'=>time(),
            'acheck'=>0,
            'astop'=>0
                ];
            $res=db("adv")
                ->insert($adv);
            if ($res) {
                $this->success("广告数据添加成功，需要进行审核才能正常使用","deal/advdeal");
                }
            else{
                
                $this->error("广告数据添加失败","deal/advdeal");
            }
        }
    }
    public function advInfo()
    {  
       
       $adv =db('adv')
       ->where('aid',input('aid'))
       ->select();

        return view('',["adv"=>$adv]);

    }
    public function advInfoModi()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $aaa=$find['aimg'];
       $file = request()->file("aimg");
       // 存储到指定路径
       if ($file) {
       $path = ROOT_PATH."public/static/img/adv/";
       $info = $file ->validate(['size'=>102400,'ext'=>'jpg,png,gif']) ->move($path);
       }
       else{
        $info=0;
       }
       
       // 
       $advtitle=input('atext');
       $advcon=input('acontent');
       $apath=input('apath');
       if($info){
            $filename = $info ->getSaveName();
            $adv = db('adv')
            ->where('aid',input('aid'))
            ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'aimg'=>$filename,'apath'=>$apath,'atime'=>time(),
                'acheck'=>0]);
            if ($adv) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this->success("广告数据修改成功，需要进行审核才能正常使用","deal/advdeal");
                }
            else{
                
                $this->error("广告数据修改失败","deal/advdeal");
            }

        }else{
            $adv = db('adv')
                ->where('aid',input('aid'))
                ->setField(['atitle'=>$advtitle,'acontent'=>$advcon,'apath'=>$apath,'atime'=>time(),
                    'acheck'=>0]);
                if ($adv) {
                    $this->success("广告数据修改成功，需要进行审核才能正常使用","deal/advdeal");
                    }
                else{
                    
                    $this->error("广告数据修改失败","deal/advdeal");
                }
        }
    }
    public function advDel()
    {  

        $find=db('adv')
        ->where('aid',input('aid'))
        ->find();


        $aaa=$find['aimg'];


        $advdel=db('adv')
        ->where('aid',input('aid'))
        ->delete();
         if ($advdel) {
                unlink(ROOT_PATH."public\static\img\adv\\".$aaa);
                $this ->success('删除广告成功',url("deal/advdeal"));
            }
            else{
                $this ->error('删除广告失败',url("deal/advdeal"));
            }

    }

    public function advOK()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['acheck'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',1);
            if ($modi) {
                 $this ->success('广告审核通过',url("deal/advdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('acheck',0);
            if ($modi) {
                 $this ->error('广告审核取消，无法正常显示',url("deal/advdeal"));
             } 

        }
    

    }
    public function advStop()
    {  
        $advcheck=db('adv')
        ->where('aid',input('aid'))
        ->find();
        $check=$advcheck['astop'];
        if ($check==0) {
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',1);
            if ($modi) {
                 $this ->success('广告暂停使用,无法正常显示',url("deal/advdeal"));
             } 
        }
        else{
            $modi=db('adv')
            ->where('aid',input('aid'))
            ->setField('astop',0);
            if ($modi) {
                 $this ->error('广告取消暂停，广告正常显示',url("deal/advdeal"));
             } 

        }
    }    
}
